package ex.hql.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ex.hql.mapper.DepartmentMapper;
import ex.hql.pojo.Department;
import ex.hql.pojo.RespBean;
import ex.hql.service.IDepartmentService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hql
 * @since 2021-08-19
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    /**
     * 获取所有部门
     * @return
     */
    @Override
    public List<Department> getALLDepartments() {
        return getBaseMapper().getALLDepartments(-1);
    }

    /**
     * 添加部门
     * @param department
     * @return
     */
    @Override
    public RespBean addDep(Department department) {
        department.setEnabled(true);
        getBaseMapper().addDep(department);
        if(1==department.getResult()){
            return RespBean.success("添加成功!",getBaseMapper().selectList(new QueryWrapper<Department>().eq("id",department.getId())));
        }
        return RespBean.error("添加失败！");
    }

    /**
     * 删除部门
     * @param id
     * @return
     */
    @Override
    public RespBean deleteDep(Integer id) {
        Department department=new Department();
        department.setId(id);
        getBaseMapper().deleteDep(department);
        if(department.getResult()==-2){
            return RespBean.error("该部门存在子部门,删除失败！");
        }
        if(department.getResult()==-1){
            return RespBean.error("该部门下存在员工，删除失败！");
        }
        if(department.getResult()==1){
            return RespBean.success("删除成功！");
        }
        return RespBean.error("删除失败！");
    }
}
